package cn.tedu.javagame.ball;

import java.awt.*;

public class Ball {
    int x,y;
    int length;
    int speed;
    Color color;
    int state ;//0左上 1右上 2右下 3左下
    public Ball(){
        this.x = (int) (Math.random()*BallFrame.WIDTH);
        this.y = (int) (Math.random()*(BallFrame.HEIGHT-320)+320);
        this.length = (int) (Math.random()*80+20);
        this.speed = (int) (Math.random()*6+2);
        int red = (int) (Math.random()*256);
        int green = (int) (Math.random()*256);
        int blue = (int) (Math.random()*256);
        this.color = new Color(red,green,blue);
        this.state = (int) (Math.random()*4);
    }
    public void move(){
        if(this.state==0){
            this.x -= this.speed;
            this.y -= this.speed;
        }else if(this.state == 1){
            this.x += this.speed;
            this.y -= this.speed;
        }else if(this.state == 2){
            this.x += this.speed;
            this.y += this.speed;
        }else if(this.state ==3){
            this.x -= this.speed;
            this.y += this.speed;
        }
        if(this.x<=0){
            if (this.state == 0){this.state=1;}
            if (this.state == 3){this.state=2;}
        }
        if(this.x+this.length+10>=BallFrame.WIDTH){
            if (this.state == 1){this.state=0;}
            if (this.state == 2){this.state=3;}
        }
        if(this.y<=0){
            if (this.state == 0){this.state=3;}
            if (this.state == 1){this.state=2;}
        }
        if(this.y+this.length+10>=BallFrame.HEIGHT){
            if (this.state == 2){this.state=1;}
            if (this.state == 3){this.state=0;}
        }
    }
    public boolean hitBrick(Brick brick){
        // 计算小球中心坐标
        int ballCenterX = this.x + this.length/2;
        int ballCenterY = this.y + this.length/2;
        // 计算砖块边界
        int brickLeft = brick.x;
        int brickRight = brick.x + brick.width;
        int brickTop = brick.y;
        int brickBottom = brick.y + brick.height;
        // 检测小球是否与砖块碰撞
        return ballCenterX >= brickLeft && ballCenterX <= brickRight &&
                ballCenterY >= brickTop && ballCenterY <= brickBottom;
    }
}
